/**
 * 
 */
package com.novartis.nims.imagedc.logic;

import java.util.TreeSet;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
 * This class contains a <code>SortedSet</code> of <code>Rule</code>s. It also
 * implements logic for determining which rules to compare a given scan against,
 * and which of the resulting matches takes precedence.
 * <p>
 * This class contains annotations from the Simple framework which define how
 * its instances can be serialized to and deserialized from XML documents.
 * 
 * @author Josh Snyder
 * @see Rule
 * @see org.simpleframework.xml
 */
@Root(name="RuleSet")
public class RuleSet {
	
	private static final String DEFAULT_SCAN_NAME = "Unidentified";
	
	//TreeSet implementation guarantees iteration in ascending order, so we
	//do not use the more generic SortedSet as the type of this field, as it
	//makes no such guarantee
	@ElementList(name="Rules")
	private TreeSet<Rule> rules = new TreeSet<Rule>();
	
	/**
	 * private since this class is only expected to be instantiated by Simple serialization
	 * framework
	 */
	private RuleSet() {}
	
	public String getMatchingScanName(Scan scan) {
		String scanName = DEFAULT_SCAN_NAME;
		
		for (Rule rule : rules) {
			if (rule.match(scan)) {
				scanName = rule.getScanName();
				break;
			}
		}
		
		return scanName;
	}

}
